<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\Article;
use Common\Models\ArticleCategory;
use Common\Models\ArticleFaquan;
use Common\Models\ArticleFaquanCate;

class ArticleController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}



    /**
     * 爆品优选后台数据库里面添加，前期写死
     * 发圈分类
     */
    public function article_cateAction(){
        $articleFaquanCate = new ArticleFaquanCate();
        $list = $articleFaquanCate->find_list();
        $new_list[-1]=['id'=>0,'name'=>'精选'];
        if($list){
            $list = array_merge($new_list,$list);
        }else{
            $list = $new_list;
        }
        $this->ok($list);
    }

    /**
     * 获取列表
     */
    public function articleinfoAction(){
        if(!isset($this->params['cid']) || empty($this->params['cid'])){
            $this->params['cid']=1;
        }
        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = Common::PAGE;
        }else{
            $page = (int)$this->params['page'];
        }

        //当前条数
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $pageSize = Common::PAGESIZE;
        }else{
            $pageSize = (int)$this->params['page_size'];
        }
        $articleFaquan = new ArticleFaquan();
        $item_list = $articleFaquan->find_list($this->params);
        foreach($item_list['items'] as $key=>$value){
            if(!empty($value['itempic'])) {
                $item_list['items'][$key]['itempic'] = explode(',', $value['itempic']);
            }else{
                $item_list['items'][$key]['itempic']=[];
            }
            $item_list['items'][$key]['copy_comment']='復·制该条评论👉🏻  (复制后自动生成口令)咑·開【tao寳】即可';
            //商品详情获取价格等信息
            $goods_detail_redis_key="goods_detail_haodanku_{$value['itemid']}";
            $goods_res=$this->cache->get_cache($goods_detail_redis_key);
            if(!$goods_res){
                $goods_res=$this->haodanku->goods_detail($value['itemid']);
            }
            $this->cache->set_cache($goods_detail_redis_key,$goods_res);
            $item_list['items'][$key]['itemtitle']=$goods_res['Title'];
            $item_list['items'][$key]['site']='haodanku';
            $item_list['items'][$key]['itemendprice']=$goods_res['EndPrice'];
            $item_list['items'][$key]['couponmoney']=$goods_res['CouponMoney'];
            $item_list['items'][$key]['commission']=round($goods_res['Commission'] * ($this->user_info['self_money']+$this->user_info['self_money_plus'])/100,2);
            $item_list['items'][$key]['shopType']=$goods_res['ShopType'];
            $item_list['items'][$key]['dummy_click_statistics']=$goods_res['MonthSale'];
        }
        if($item_list['items']){
            $this->ok($item_list['items']);
        }else{
            $this->ok([],-201,'列表没有更多消息');
        }
//        $data['items'][0]['itemid']="587119502358";
//        $data['items'][0]['content']='天气越来越冷了$普通表情[10001]$来煲个汤吧\r\n美的$emoji表情[43]$家用全自动电炖锅\r\n聚划算加券到手$emoji表情[158]$79\r\n1.8L容量$emoji表情[393]$9小时预约\r\n晚上放料，早上起床就能喝热乎的$emoji表情[6]$';
//        $data['items'][0]['itempic']=[
//                "http://img04.taobaocdn.com:80/tfscom/TB1s0pGnHj1gK0jSZFuXXcrHpXa",
//                "https://img.alicdn.com/imgextra/i2/3426460083/O1CN01c9p58y1CU3PqDzdFV_!!3426460083.jpg",
//                "https://img.alicdn.com/imgextra/i4/3426460083/O1CN01XAEW4s1CU3Pn5K349_!!3426460083.jpg",
//                "http://img03.taobaocdn.com:80/tfscom/TB1FIXCnFP7gK0jSZFjXXc5aXXa"
//        ];
//        $data['items'][0]['dummy_click_statistics']="373";
//        $data['items'][0]['copy_comment']='復·制该条评论👉🏻  (复制后自动生成口令)咑·開【📱🍑 tao寳】即可';
//        $data['items'][0]['itemendprice']='89.00';
//        $data['items'][0]['commission']=9.43;

    }




    /**
     * @func 文章列表
     * @param type 文章分类
     */
	public function article_listAction(){
		$data = [];
		$param=$this->params;
        if(isset($param['type']) && !empty($param['type'])){
            $data['type'] = $param['type'];
        }else{
            $data['type'] = 0;
        }

        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $this->params['page'] = Common::PAGE;
        }
        //当前条数
        if(!isset($this->params['pageSize']) || empty($this->params['pageSize'])){
            $this->params['pageSize'] = Common::PAGESIZE;
        }

        $model = new Article();
        $article_list['items'] = $model->getList($data);
		foreach($article_list['items'] as $k =>$v){
            $article_list['items'][$k]['images']=explode(',',$v['images']);
		}
		if(empty($article_list['items'])){
		    $this->ok($article_list,-201,'列表没有更多数据');
        }
        $this->ok($article_list);
	}

    /**
     * @func 文章详情
     */
    public function article_detailAction(){
        $Article=new Article();
        $id=$this->params['id'];
        $ret=$Article->FindByid($id);
        $this->ok($ret);
    }
    /**
     * @func 文章分类
     */
    public function article_categoryAction(){
        $model = new ArticleCategory();
        $params=$this->params;
        $list = $model->getList($params);
        $this->ok($list);
    }
}
